
// MFCPokerView.cpp : implementation of the CMFCPokerView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "MFCPoker.h"
#endif

#include "MFCPokerDoc.h"
#include "MFCPokerView.h"
#include <sstream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCPokerView

IMPLEMENT_DYNCREATE(CMFCPokerView, CView)

BEGIN_MESSAGE_MAP(CMFCPokerView, CView)
	ON_COMMAND(IDC_GO_BTN,OnCommandGoBtn)
END_MESSAGE_MAP()

// CMFCPokerView construction/destruction

CMFCPokerView::CMFCPokerView()
{
	// TODO: add construction code here

}

CMFCPokerView::~CMFCPokerView()
{
}

BOOL CMFCPokerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CMFCPokerView drawing

void CMFCPokerView::OnDraw(CDC* pDC)
{

	CMFCPokerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	CButton *goButton=new CButton;
	goButton->Create(_T("Go"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(100,0,130,30), this, 0x302);

	

	std::vector<player> players=pDoc->getPlayers();

	for (int pNum=0;pNum<(int)players.size();++pNum){
		for (int cNum=0;cNum<2;++cNum){

			int imgID=(players[pNum].getCards())[cNum].imgID();
			CString imgPath;
			imgPath.Format(_T ("cards\\%d.bmp"), imgID);



			CString szFilename(imgPath);
			HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename,
										IMAGE_BITMAP,0,0,
										LR_LOADFROMFILE|LR_CREATEDIBSECTION);
 
			CBitmap bmp;
			bmp.Attach(hBmp);
   
		//	CClientDC dc(this);
			CDC bmDC;
			bmDC.CreateCompatibleDC(pDC);
			CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
 
			BITMAP  bi;
			bmp.GetBitmap(&bi);
 
			pDC->BitBlt(80*cNum,100*pNum+40,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
 
			bmDC.SelectObject(pOldbmp);
			bmp.Detach();

		}

	}




		

	std::vector<card> groundCards=pDoc->getGroundCards();
	for (int gNum=0;gNum<(int)groundCards.size();++gNum){
		int imgID=groundCards[gNum].imgID();
		CString imgPath;
		imgPath.Format(_T ("cards\\%d.bmp"), imgID);



		CString szFilename(imgPath);
		HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename,
									IMAGE_BITMAP,0,0,
									LR_LOADFROMFILE|LR_CREATEDIBSECTION);
 
		CBitmap bmp;
		bmp.Attach(hBmp);
   
	//	CClientDC dc(this);
		CDC bmDC;
		bmDC.CreateCompatibleDC(pDC);
		CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
 
		BITMAP  bi;
		bmp.GetBitmap(&bi);
 
		pDC->BitBlt(200+80*gNum,40,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
 
		bmDC.SelectObject(pOldbmp);
		bmp.Detach();

	}


	ReleaseDC(pDC);

	// TODO: add draw code for native data here
}


void CMFCPokerView::OnCommandGoBtn(){

	static int counter=1;
	CMFCPokerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	if(counter==1){
		pDoc->flop();
		++counter;
	}
	else if(counter==2){
		pDoc->turn();
		++counter;
	}
	else if(counter==3){
		pDoc->river();
		++counter;
	}

//	CRect rect(520,40,650,240);
//	InvalidateRect(rect);
	Invalidate();
	UpdateWindow();

/*	int winnerP=pDoc->winner();

	CString s;
	s.Format(_T ("Player %d won"), winnerP);

	CRect rect;
	GetClientRect (&rect);
	pDC->DrawText(s, -1, &rect, DT_SINGLELINE);
*/
}

// CMFCPokerView diagnostics

#ifdef _DEBUG
void CMFCPokerView::AssertValid() const
{
	CView::AssertValid();
}

void CMFCPokerView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMFCPokerDoc* CMFCPokerView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCPokerDoc)));
	return (CMFCPokerDoc*)m_pDocument;
}
#endif //_DEBUG


// CMFCPokerView message handlers


void CMFCPokerView::OnInitialUpdate()
{
	CView::OnInitialUpdate();
	int nPlayers;
	PokerDialog dlg;
	if (dlg.DoModal()==IDOK)
		nPlayers=dlg.numPlayers;


	CMFCPokerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	pDoc->play(nPlayers);
	pDoc->preflop();


	// TODO: Add your specialized code here and/or call the base class
}